package sk.fiit.vppj.eventmanager.volunteer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;



import sk.fiit.vppj.eventmanager.core.DatabaseManager;
import sk.fiit.vppj.eventmanager.core.UserManager;
import sk.fiit.vppj.eventmanager.core.Utils;
import sk.fiit.vppj.eventmanager.data.BasicUser;
/**
 * Table model which manipulates with table in administrator's general view tab
 * @author Marek
 *
 */
final class BasicUserTableModel extends DefaultTableModel {
	private String[] columnNames = { "ID", "Name", "Time", "Class" };
	private List<BasicUser> data = null;
	private static final int PAGINATION = 25;

	public BasicUserTableModel() {
	}

	@Override
	public int getRowCount() {
		if( this.data == null ) {
			return 0;
		}
		return this.data.size();
	}

	@Override
	public int getColumnCount() {
		return this.columnNames.length;
	}

	@Override
	public String getColumnName(int columnIndex) {
		return this.columnNames[columnIndex];
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return getValueAt( 0, columnIndex ).getClass();
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if( columnIndex == 0 ) {
			return data.get( rowIndex ).getId();
		}
		else if( columnIndex == 1 ) {
			return data.get( rowIndex ).getName();
		}
		else if( columnIndex == 2 ) {
			return data.get( rowIndex ).getDatetime();
		}
		else if( columnIndex == 3 ) {
			return data.get( rowIndex ).getClazz();
		}
		else {
			return data.get( rowIndex ).getId();
		}
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addTableModelListener(TableModelListener l) {

	}

	@Override
	public void removeTableModelListener(TableModelListener l) {
		// TODO Auto-generated method stub

	}

	public int getPagination() {
		return PAGINATION;
	}


	private String getExtraSearchOpt(String exp) {
		String extSearch = "";
//		if( exp.length() == 0 ) {
//			extSearch = "";
//		}
//		else if( Utils.isNumber( exp ) ) {
//			extSearch = "AND u.id = ? ";
//		}
//		else {
//			extSearch = "AND u.name LIKE CONCAT('%',?,'%') ";
//		}
		return extSearch;
	}

	private PreparedStatementCreator prepareStatement(final String exp, final int page, final int activity_id) {
		final String sql = "SELECT u.id, u.name, ux.class, a.datetime, ux.group_id,  ux.event_id FROM jos_event_manager_activity_log AS a JOIN jos_users AS u ON u.id = a.user_id " +
				" JOIN jos_event_manager_users AS ux ON u.id = ux.user_id WHERE a.activity_id = ? " + getExtraSearchOpt( exp )
				+ " ORDER BY a.IDActivityLog DESC LIMIT ?, ?";

		return new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement( sql );
				ps.setInt( 1, activity_id );
				if( exp.length() == 0 ) {
					ps.setInt( 2, page * PAGINATION );
					ps.setInt( 3, PAGINATION );
				}
				else if( Utils.isNumber( exp ) ) {
					ps.setInt( 2, page * PAGINATION );
					ps.setInt( 3, PAGINATION );
				}
				else {
					ps.setInt( 2, page * PAGINATION );
					ps.setInt( 3, PAGINATION );
				}
				return ps;
			}
		};
	}

	public void prepareData(final String exp, final int page, int activity_id ) {

		this.data = DatabaseManager.getInstance().getJdbcTemplate().query( prepareStatement( exp, page, activity_id ), new RowMapper<BasicUser>() {

			public BasicUser mapRow(ResultSet rs, int rowNum) throws SQLException {
				BasicUser tempUser = new BasicUser();
				tempUser.setId( rs.getInt( "id" ) );
				tempUser.setName( rs.getString( "name" ) );
				tempUser.setDatetime( rs.getString( "datetime" ) );
				tempUser.setClazz( rs.getString( "class" ) );
				tempUser.setEvent_id( rs.getInt( "event_id" ) );
				tempUser.setGroup_id( rs.getInt( "group_id" ) );

				return tempUser;
			}

		} );
	}

}
